package com.cqupt.service.impl;

import com.cqupt.common.Pagination;
import com.cqupt.dao.TbUserMapper;
import com.cqupt.pojo.TbUser;
import com.cqupt.pojo.TbUserExample;
import com.cqupt.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private TbUserMapper tbuserMapper;

    @Override
    public TbUser queryUser(TbUser user) {
        TbUser tbuser = null;

        TbUserExample example = new TbUserExample();
        if(user!=null && user.getUserName()!=null) {
            example.createCriteria().andUserNameEqualTo(user.getUserName());
        }
        List<TbUser> list = tbuserMapper.selectByExample(example);
        if(list.size()>0) {
            tbuser = list.get(0);
        }
        return tbuser;
    }

    // 查询用户列表
    public List queryList() {
        Page<Object> page = PageHelper.startPage(3, 5, true);
        List list = tbuserMapper.selectByExample(null);
        System.out.println("数据总条数:" + page.getTotal());
        System.out.println("总页数：" + page.getPages());
        System.out.println("当前第几页：" + page.getPageNum());
        System.out.println("每页条数：" + page.getPageSize());
        //page.getResult()
        return list;
    }

    // 查询用户列表-分页数据
    public Page queryPageList(Pagination pagination) {
        Page<Object> page = PageHelper.startPage(pagination.getPage(), pagination.getLimit(), true);
        List list = tbuserMapper.selectByExample(null);
        return page;
    }

    // 添加用户
    public int insertUser(TbUser tbuser) {
        return tbuserMapper.insertSelective(tbuser);
    }

    // 添加用户
    public int deleteUser(Integer userid) {
        return tbuserMapper.deleteByPrimaryKey(userid);
    }

    // 根据用户编号查询用户
    public TbUser queryUser(Integer userid) {
        return tbuserMapper.selectByPrimaryKey(userid);
    }

    // 编辑用户
    public int updateUser(TbUser tbuser) {
        return tbuserMapper.updateByPrimaryKeySelective(tbuser);
    }
}
